require "rest_client"
require "json"

module Avod
	

	class Client

		BASE_URL = "https://cn.avoscloud.com/"
		VERSION_CODE = "1"
		API_URL = BASE_URL + VERSION_CODE + "/"
		API_OBJECT_URL = API_URL + "classes/"
		API_USER_URL = API_URL + "users"
		API_INSTALLIIONS_URL = API_URL + "installations"

		def initialize(app_id, app_key, master_key)
			@app_id = app_id
			@app_key = app_key
			@master_key = master_key
		end


		# /1/classes/<className>	POST	创建对象
		# /1/classes/<className>/<objectId>	GET	获取对象
		# /1/classes/<className>/<objectId>	PUT	更新对象
		# /1/classes/<className>	GET	查询对象
		# /1/classes/<className>/<objectId>	DELETE	删除对象


		def get_all_object(name)
			get API_OBJECT_URL + name
		end

		def create_object(class_name, object)
			post API_OBJECT_URL + class_name, object.to_json
		end

		def get_object(class_name, object_id)
			get  API_OBJECT_URL + class_name + "/"  + object_id
		end

		def update_object(class_name, object_id, object)
			put API_OBJECT_URL + class_name + "/" + object_id, object.to_json
		end

		def delete_object(class_name, object_id)
			delete API_OBJECT_URL + class_name + "/" + object_id
		end


		# URL	HTTP	功能
		# /1/users	POST	用户注册
		# 用户连接
		# /1/login	GET	用户登录
		# /1/users/<objectId>	GET	获取用户
		# /1/users/<objectId>	PUT	更新用户
		# 用户连接
		# 验证Email
		# /1/users	GET	查询用户
		# /1/users/<objectId>	DELETE	删除用户
		# /1/requestPasswordReset	POST	请求密码重设

		def update_user(user_id, params, session) #untest
			url = API_USER_URL + user_id
			result =  RestClient.put url , params,
			:"content-type" => "application/json", 
			:"X-AVOSCloud-Application-Id" => @app_id,
			:"X-AVOSCloud-Application-Key" => @app_key,
			:"X-AVOSCloud-Session-Token" => session
			return to_json(params)
		end

		def get_all_users 
			get API_USER_URL
		end

		def get_user(object_id)  
			get API_USER_URL + "/" +  object_id
		end

		def register_user(username, password, email, *opt) #untest
			if opt 
				params = opt
			else	
				params = Hash.new
			end
			params[:password] = password
			params[:username] = username			
			post API_USER_URL, params
		end


		def  rest_password(email) #untest
			params = {"email" => email}
			post API_URL + "requestPasswordReset", params
		end

		def delete_user(user_id, session) #untest
			url = API_USER_URL + user_id
			result =  RestClient.delete url ,
			:"content-type" => "application/json", 
			:"X-AVOSCloud-Application-Id" => @app_id,
			:"X-AVOSCloud-Application-Key" => @app_key,
			:"X-AVOSCloud-Session-Token" => session
			result.code == 200
		end

		def reset_email(email) #faild
			url = "https://cn.avoscloud.com/1/requestPasswordReset"
			params = {"email" => email}
			result =  RestClient.post url , params, 
			:"content-type" => "application/json", 
			:"X-AVOSCloud-Application-Id" => @app_id,
			:"X-AVOSCloud-Application-Key" => @app_key,
			:"X-AVOSCloud-Master-Key" => @master_key
			result.code == 200
		end







		# URL	HTTP	功能
		# /1/roles	POST	创建角色
		# /1/roles/<objectId>	GET	获取角色
		# /1/roles/<objectId>	PUT	更新角色
		# /1/roles	GET	查询角色
		# /1/roles/<objectId>	DELETE	删除角色



		# URL	HTTP	功能
		# /1/push	POST	推送通知


		# 安装数据

		# URL	HTTP	功能
		# /1/installations	POST	上传安装数据
		# /1/installations/<objectId>	GET	获取安装数据
		# /1/installations/<objectId>	PUT	更新安装数据
		# /1/installations	GET	查询安装数据
		# /1/installations/<objectId>	DELETE	删除安装数据

		def get_installations
			private_get  API_INSTALLIIONS_URL 
		end

		def get_install(object_id) 
			get API_INSTALLIIONS_URL + "/" + object_id
		end

		def update_install(object_id, params) #untest
			put API_INSTALLIIONS_URL + "/" + params["objectId"], params
		end
 
	
	


		# Cloud函数

		# URL	HTTP	功能
		# /1/functions	POST	调用Cloud Code函数


		protected


		def to_json(str)
			return JSON.parse(str)
		end


		def private_post(url, params)
			result =  RestClient.post url , params,
			:"content-type" => "application/json", 
			:"X-AVOSCloud-Application-Id" => @app_id,
			:"X-AVOSCloud-Application-Key" => @app_key,
			:"X-AVOSCloud-Master-Key" => @master_key
			to_json(result)

		end

		def post(url, params)
			result =  RestClient.post url , params,
			:"content-type" => "application/json", 
			:"X-AVOSCloud-Application-Id" => @app_id,
			:"X-AVOSCloud-Application-Key" => @app_key
			to_json(result)

		end

		def get(url)
			result =  RestClient.get url ,
			:"content-type" => "application/json", 
			:"X-AVOSCloud-Application-Id" => @app_id,
			:"X-AVOSCloud-Application-Key" => @app_key
			to_json(result)
		end


		def private_get(url)
			result =  RestClient.get url ,
			:"content-type" => "application/json", 
			:"X-AVOSCloud-Application-Id" => @app_id,
			:"X-AVOSCloud-Application-Key" => @app_key,
			:"X-AVOSCloud-Master-Key" => @master_key
			to_json(result)
		end

		def delete(url)
			result =  RestClient.delete url ,
			:"content-type" => "application/json", 
			:"X-AVOSCloud-Application-Id" => @app_id,
			:"X-AVOSCloud-Application-Key" => @app_key
			result.code == 200
		end

		def put(url, params)
			result =  RestClient.put url , params,
			:"content-type" => "application/json", 
			:"X-AVOSCloud-Application-Id" => @app_id,
			:"X-AVOSCloud-Application-Key" => @app_key
			to_json(result)
		end




	end
end








